/*******************************************************************************
Copyright (C), 200{x}, Borqs Tech. Co., Ltd.
File Name:
    lapi_comm.h
Description:

History:
    Date          Author                         Comments
    -----------   ---------------------------    --------------------
    Mar-20-2008   anle.zhou@borqs.com            Created
    Feb-02-2009   anle.zhou@borqs.com            Android migration
*******************************************************************************/

#ifndef LAPI_COMM_H
#define LAPI_COMM_H

#include <stdio.h>
#include <stdlib.h>

#include "lapi.h"

#define LAPISRV_VERSION "1.0.0 625"

#ifdef LAPI_ON_ANDROID
#include "lapi_log.h"
#define LAPI_DBG(fmt, arg...) lapi_log(LOG_LEVEL_DEBUG, "<LAPI DBG> "fmt"\n", ##arg)
#define LAPI_INF(fmt, arg...) lapi_log(LOG_LEVEL_INFO, "<LAPI INF> "fmt"\n", ##arg)
#define LAPI_ERR(fmt, arg...) lapi_log(LOG_LEVEL_ERROR, "<LAPI ERR> "fmt"\n", ##arg)
#define LAPI_QUIT(fmt, arg...)  do { \
                                    lapi_log(LOG_LEVEL_FATAL, "<LAPI QUIT> "fmt, ##arg); \
                                    exit(1); \
                                } while (0);
#else
#define LAPI_DBG(fmt, arg...) fprintf(stdout, "<LAPI DBG> "fmt"\n", ##arg)
#define LAPI_INF(fmt, arg...) fprintf(stdout, "<LAPI INF> "fmt"\n", ##arg)
#define LAPI_ERR(fmt, arg...) fprintf(stderr, "<LAPI ERR> "fmt"\n", ##arg)
#define LAPI_QUIT(fmt, arg...)  do { \
                                    fprintf(stderr, "<LAPI QUIT> "fmt"\n", ##arg); \
                                    exit(1); \
                                } while (0);
#endif

#ifndef LAPI_MIN
#define LAPI_MIN(a,b)    (((a) <= (b)) ? (a) : (b))
#endif

#ifndef LAPI_MAX
#define LAPI_MAX(a,b)    (((a) >= (b)) ? (a) : (b))
#endif

#define LAPI_CLIENT_MAX 4

#if 0
Move to lapi.h
/* basic type */
typedef unsigned int LAPI_UINT32;
typedef unsigned short LAPI_UINT16;
typedef unsigned char LAPI_UINT8;
typedef int LAPI_INT32;
typedef short LAPI_INT16;
typedef char LAPI_INT8;
typedef unsigned char LAPI_BOOL;
typedef unsigned long long LAPI_UINT64;
typedef float LAPI_FLOAT;
typedef double LAPI_DOUBLE;

#define AGPS_SRV_ADDR_LEN (15+1)
#define AGPS_IMSI_LEN (15 + 1)
#define AGPS_LAC_LEN (4 + 1)
#define AGPS_CID_LEN (4 + 1)
#define AGPS_GPRSIF_LEN 8 // GPRS interface like: ccinet0
#define MAX_OTAMSG_LEN (256)
#define REQUESTERID_LEN (32)
#define CLIENTNAME_LEN (64)
#define MAX_SATELLITE (12)
#endif

/*#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif*/

/* error code */
typedef enum _LAPI_RESULT_E {
    LAPI_RESULT_OK = 0,
    LAPI_RESULT_UNKNOWN, // unknown/internal error
    LAPI_RESULT_LPL_ERR, // SiRF LPL layer error
    LAPI_RESULT_BAD_REQ,  // bad request
    LAPI_RESULT_BAD_SESSIONID,  // bad session ID
    LAPI_RESULT_OPENSESSION_FAILED, // failed to open session
    LAPI_RESULT_SESSION_FULL, // failed to open session
    LAPI_RESULT_TIMEOUT, // request timeout
    LAPI_RESULT_FIX_INVALID, // invalid location fix
} LAPI_RESULT_E;

#if 0
Move to lapi.h
/* gps satellite infomation */
typedef struct _SATELLITE_INFO_S {
    LAPI_UINT8   satelliteId;
    LAPI_UINT8   signalStrength;
    LAPI_UINT16  azimuth;
    LAPI_UINT8   elevationAngle;
} __attribute__((packed)) SATELLITE_INFO_S;


/* location fix */
typedef struct _LAPI_LOCATION_S {
    LAPI_DOUBLE longitude;
    LAPI_DOUBLE latitude;
    LAPI_FLOAT altitude;
    LAPI_UINT16 bearing;
    LAPI_UINT16 speed;
    LAPI_UINT64 timeStamp;
    LAPI_UINT16 ttff;
    LAPI_UINT16 accuracy;
    LAPI_UINT16 num_satellite;
    SATELLITE_INFO_S satellite_info[MAX_SATELLITE];
} __attribute__((packed)) LAPI_LOCATION_S;
#endif

/* lpl settings */
/*typedef struct _LAPI_CONF_S {
    LAPI_UINT8 method; // location method
    LAPI_UINT8 reset;  // reset type
    char srv_addr[AGPS_SRV_ADDR_LEN];
    LAPI_UINT32 srv_port;
    struct {
        LAPI_UINT32 hor_accuracy;
        LAPI_UINT32 ver_accuracy;
        LAPI_UINT32 timeout;
    } qos;
    LAPI_UINT32 interval;
    char imsi[AGPS_IMSI_LEN];
    char lac[AGPS_LAC_LEN];
    char cid[AGPS_CID_LEN];
    char gprs_inf[AGPS_GPRSIF_LEN];
} __attribute__((packed)) LAPI_CONF_S;*/

#if 0
Move to lapi.h
/* MTLR OTA message structure */
typedef struct _LAPI_OTAMSG_S {
    LAPI_UINT32 length;
    LAPI_UINT32 data[MAX_OTAMSG_LEN];
} __attribute__((packed)) LAPI_OTAMSG_S;

/* mtlr user response type */
typedef struct _LAPI_MTLR_USR_RSP
{
    int notification;
    int verification;
    LAPI_UINT8 requesterId[REQUESTERID_LEN];
    LAPI_UINT8 clientName[CLIENTNAME_LEN];
} __attribute__((packed)) LAPI_MTLR_USR_RSP;
#endif

/* mtlr user response type */
typedef struct _THREAD_LPL_ARG_tag
{
    LAPI_INT32 fd;
    LAPI_INT32 sessionID;
    LAPI_UINT8 type;
    LAPI_OTAMSG_S *msg;
} __attribute__((packed)) THREAD_LPL_ARG;

typedef enum _LAPI_LISTENER_TYPE_E {
    LAPI_UNKNOWN_LISTENER = 0,
    LAPI_LOCATION_LISTENER = 1,
    LAPI_NMEA_LISTENER = 2,
    LAPI_SUPLINIT_LISTENER = 4,
    LAPI_GPRSCLOSE_LISTENER = 8
} LAPI_LISTENER_TYPE_E;

typedef struct _LAPI_CLIENT_S {
    LAPI_INT32 sessionID;
    LAPI_INT32 syncFd;
    LAPI_INT32 asyncFd;
    LAPI_INT32 used;
    //LAPI_LISTENER_TYPE_E type;
    LAPI_UINT8 type;
} LAPI_CLIENT_S;

#endif /* LAPI_COMM_H */
